package ru.spb.nicetu.jdbc_project;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by dchernyshov on 10.03.15.
 */
public class MainFrame extends JFrame {
    private JTable table;
    private TblModel tblModel;
    private JButton btnPrevious;
    private JButton btnNext;

    public MainFrame() {

        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;

        setSize(screenWidth / 4, screenHeight / 2);
        setLocationByPlatform(true);

        // create first horizontal box
        tblModel = new TblModel();
        table = new JTable(tblModel);
        JScrollPane scrollPane = new JScrollPane(table);
        table.setMaximumSize(table.getMaximumSize());
       // table.setSize((int)table.getPreferredSize().getWidth(), screenHeight / 2 -20);

        Box hbox1 = Box.createHorizontalBox();
        hbox1.add(scrollPane);

        // create second horizontal box
        Box hbox2 = Box.createHorizontalBox();
        btnPrevious = new JButton("Previous");
        btnPrevious.addActionListener(new BtnPreviousListener());
        hbox2.add(btnPrevious);
        btnNext = new JButton("Next");
        btnNext.addActionListener(new BtnNextListener());
        hbox2.add(Box.createHorizontalStrut(20));
        hbox2.add(btnNext);

        //add all horizontal box into vertical box

        Box vbox = Box.createVerticalBox();
        vbox.add(hbox1);
        vbox.add(Box.createVerticalStrut(10));
        vbox.add(hbox2);

        add(vbox, BorderLayout.CENTER);
    }

    public class BtnNextListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            TblModel.incrementCountForPages();
            tblModel.fireTableDataChanged();
        }
    }

    public class BtnPreviousListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            TblModel.decrementCountForPages();
            tblModel.fireTableDataChanged();
        }
    }
}
